﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Users
/// </summary>
public class UsersRepository
{
    private Up1VNDataContext db;
    public UsersRepository()
	{
        db = new Up1VNDataContext();
	}

    public int GetUserIdByName(string username)
    {
        var a= (from o in db.Users where String.Equals(o.Users_Name,username) select o).SingleOrDefault();
        return (int)a.Users_Id;
    }
    public int GetIdByEmailOrUsername(string user_email)
    {
        return (int)(from o in db.Users where String.Equals(o.Users_Name, user_email) || String.Equals(o.Users_Email, user_email) select o).SingleOrDefault().Users_Id;
    }
    public string GetEmailById(int userId)
    {
        return (from o in db.Users where o.Users_Id == userId select o).SingleOrDefault().Users_Email;
    }

    public UserConfiguration GetConfigurationByUserId(int userId)
    {
        return (from o in db.UserConfigurations where o.UserId == userId select o).SingleOrDefault();
    }
    public bool UpdateConfiguration(int id, bool ReceiveEmail, bool ReceiveFromAdmin, bool ReceiveFromUser)
    {
        var a = (from o in db.UserConfigurations where o.Id == id select o).SingleOrDefault();
        if (a != null)
        {
            a.ReceiveEmail = ReceiveEmail;
            a.ReceiveMessagefromAdmin = ReceiveFromAdmin;
            a.ReceiveMessagefromUser = ReceiveFromUser;
            db.SubmitChanges();
            return true;
        }
        else
            return false;
    }

    ///
    // Favorites List
    ///
    public List<UserFavorite> GetFavoritesByUserId(int userId)
    {
        return (from o in db.UserFavorites where o.UserId == userId select o).ToList();
    }

    public bool DeteleFavorite(int Id)
    {
        var a = (from o in db.UserFavorites where o.Id == Id select o).SingleOrDefault();
        if (a != null)
        {
            db.UserFavorites.DeleteOnSubmit(a);
            db.SubmitChanges();
            return true;
        }
        else
            return false;
    }
}